package com.riven.bilibilidemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.riven.bilibilidemo.R;
import com.riven.bilibilidemo.activity.FanJvActivity;
import com.riven.bilibilidemo.bean.BangumiBean;

/**
 * Created by Pooh on 2017/3/11.
 */
public class MyFristAdater extends RecyclerView.Adapter<MyFristAdater.MyFristViewHodler> implements View.OnClickListener {
    private int positionMy;
    Context context;
    BangumiBean bean;

    public MyFristAdater(Context context, BangumiBean bean,int position) {
        this.context = context;
        this.bean = bean;
        this.positionMy = position;
    }

    @Override
    public MyFristViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.bangumi_recycle,parent,false);
        MyFristViewHodler hodler = new MyFristViewHodler(view);
        hodler.recycle_img.setOnClickListener(this);
        return hodler;
    }

    @Override
    public void onBindViewHolder(MyFristViewHodler holder, int position) {
        if (positionMy==1){
            holder.recycle_name.setText(bean.getResult().getPrevious().getList().get(position).getTitle());
            holder.recycle_new.setVisibility(View.GONE);
            ImageLoader.getInstance().displayImage(bean.getResult().getPrevious().getList().get(position).getCover(),holder.recycle_img);

        }else {
            holder.recycle_name.setText(bean.getResult().getSerializing().get(position).getTitle());
            holder.recycle_new.setText("更新至第"+bean.getResult().getSerializing().get(position).getNewest_ep_index()+"话");
            ImageLoader.getInstance().displayImage(bean.getResult().getSerializing().get(position).getCover(),holder.recycle_img);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, FanJvActivity.class);
        context.startActivity(intent);
    }

    @Override
    public int getItemCount() {
        if (positionMy==1){
            return bean==null?0:bean.getResult().getPrevious().getList().size();
        }else {
            return bean==null?0:bean.getResult().getSerializing().size();
        }

    }

    public class MyFristViewHodler extends RecyclerView.ViewHolder {

        public TextView recycle_name;
        public TextView recycle_new;
        public ImageView recycle_img;

        public MyFristViewHodler(View itemView) {
            super(itemView);
            recycle_img = (ImageView) itemView.findViewById(R.id.recycle_img);
            recycle_new = (TextView) itemView.findViewById(R.id.recycle_new);
            recycle_name = (TextView) itemView.findViewById(R.id.recycle_name);

        }
    }
}
